package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.RoleService;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description UserController
 * @Author KangChi Wang
 * @Date 2024-09-04
 */
@RestController
@Slf4j
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页",notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.username","userDto.email","userDto.dataState","userDto.deptNo"})
    public ResponseResult<PageResponse<UserVo>> findUserVoPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> userVoPage = userService.page(userDto, pageNum, pageSize);
        return ResponseResult.success(userVoPage);
    }

    @ApiOperation("启用和禁用")
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult isEnable(@PathVariable Long id, @PathVariable String status){
        userService.isEnable(id,status);
        return ResponseResult.success();
    }
    @ApiOperation("用户添加")
    @PutMapping
    public ResponseResult<Boolean> save(@RequestBody UserDto userDto){
       Boolean flag= userService.save(userDto);
        return ResponseResult.success(flag);
    }
    @ApiOperation("修改")
    @PatchMapping
    public ResponseResult<Boolean> update(@RequestBody UserDto userDto){
        Boolean flag= roleService.updateUser(userDto);
        return ResponseResult.success(flag);
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{userId}")
    public ResponseResult remove(@PathVariable Long userId){
        userService.remove(userId);
        return ResponseResult.success();
    }
}
